<template>
  <div class="person">
    <h1>情况五：监视以上多个数据</h1>
    <h2>name: {{ person.name }}</h2>
    <h2>age: {{ person.age }}</h2>
    <h2>car: {{ person.car.c1 }} | {{ person.car.c2 }}</h2>
    <button @click="changeName">changeName</button>
    <button @click="changeAge">changeAge</button>
    <button @click="changeFirstCar">changeFirstCar</button>
    <button @click="changeSecondCar">changeSecondCar</button>
    <button @click="changeCar">changeCar</button>
  </div>
</template>

<script lang="ts" setup name="Person">
import { watch, reactive } from "vue";
// 1. data
let person = reactive({
  name: "Jack",
  age: 18,
  car: {
    c1: "奔驰",
    c2: "宝马",
  },
});
// 2. function
function changeName() {
  person.name += "~";
}
function changeAge() {
  person.age += 1;
}
function changeFirstCar() {
  person.car.c1 = "比亚迪";
}
function changeSecondCar() {
  person.car.c2 = "大众";
}
function changeCar() {
  person.car = { c1: "长城", c2: "奇瑞" };
}

// 3. watch
// 情况五： 修改以上的数据类型的组合
watch([() => person.name, () => person.car.c1], (newVal, oldVal) => {
  console.log("data is change", newVal, oldVal);
});
</script>

<style scoped>
.person {
  background-color: skyblue;
  box-shadow: 0 0 10px;
  border-radius: 10px;
  padding: 20px;
}
button {
  margin: 0 5px;
}
</style>
